﻿using System;
using System.Data;
using TimKen.DataAccess;
using TimKen.DataEntity;
using System.Collections.Generic;

namespace TimKen.BussLogic
{
    /// <summary>
    /// 购物车业务逻辑操作类
    /// </summary>
    public class ShoppingCartManager
    {
        private readonly TimKen.DataAccess.ShoppingCartDAO dal = new TimKen.DataAccess.ShoppingCartDAO();
        public ShoppingCartManager()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string UserName, int ProductID)
        {
            return dal.Exists(UserName, ProductID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.ShoppingCart model)
        {
            if (Exists(model.UserName, model.ProductID))
            {
                ShoppingCart cart = GetModel(model.UserName, model.ProductID);
                cart.Number = cart.Number + model.Number;
                dal.Update(cart);
            }
            else
                dal.Add(model);

        }


        public int GetProductCountInCart(string UserName)
        {
            string sql = "SELECT  SUM(Number) FROM  ShoppingCart WHERE UserName='{0}'";
            sql = string.Format(sql, UserName);
            object result =   DbHelperSQL.GetSingle(sql);

            if (result == null)
                return 0;
            return Convert.ToInt32(result);

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.ShoppingCart model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string UserName, int ProductID)
        {

            dal.Delete(UserName, ProductID);
        }
        /// <summary>
        /// 清空购物车
        /// </summary>
        /// <param name="userName"></param>
        public void CleanCart(string userName) 
        {
            dal.CleanCart(userName);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.ShoppingCart GetModel(string UserName, int ProductID)
        {

            return dal.GetModel(UserName, ProductID);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }




        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// Tables[0] 数据 Tables[1] totle
        /// </summary>
        public DataSet GetPageList(int PageSize, int PageIndex, string userName, int languageID)
        {
            return dal.GetPageList(PageSize, PageIndex, userName, languageID);
        }

        #endregion  成员方法
    }
}
